package  main.java.leetcode.editor.cn;
//2022-03-20 22:00:41
//给你一棵二叉树的根节点 root ，返回其节点值的 后序遍历 。 
//
// 
//
// 示例 1： 
//
// 
//输入：root = [1,null,2,3]
//输出：[3,2,1]
// 
//
// 示例 2： 
//
// 
//输入：root = []
//输出：[]
// 
//
// 示例 3： 
//
// 
//输入：root = [1]
//输出：[1]
// 
//
// 
//
// 提示： 
//
// 
// 树中节点的数目在范围 [0, 100] 内 
// -100 <= Node.val <= 100 
// 
//
// 
//
// 进阶：递归算法很简单，你可以通过迭代算法完成吗？ 
// Related Topics 栈 树 深度优先搜索 二叉树 
// 👍 790 👎 0

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

class BinaryTreePostorderTraversal {
    public static void main(String[] args) {
        //创建该题目的对象方便调用
        Solution solution = new BinaryTreePostorderTraversal().new Solution();
        TreeNode root = new TreeNode(1);
        TreeNode node_2 = new TreeNode(2);
        TreeNode node_3 = new TreeNode(3);
        root.left = null;
        root.right = node_2;
        node_2.left = node_3;

        solution.postorderTraversal(root);
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {

        List<Integer> result = new LinkedList<>();
//        postorder(root,result);
        Deque<TreeNode> stack = new LinkedList<>();

        TreeNode node = root;
        TreeNode pre = null;

        while (node != null || !stack.isEmpty()){

            while (node != null){
                stack.push(node);
                node = node.left;
            }

            node = stack.pop();

            if(node.right == null || node.right == pre){
                result.add(node.val);
                pre = node;
                node = null;
            }else {
                stack.push(node);
                node = node.right;
            }
        }

//        while (root != null || !stack.isEmpty()){
//
//            while (root != null){
//                stack.push(root);
//                root = root.left;
//            }
//
//            root = stack.pop();
//
//            if(root.right == null || root.right == pre){
//                result.add(root.val);
//                pre = root;
//                root = null;
//            }else {
//                stack.push(root);
//                root = root.right;
//            }
//        }

        return result;
    }

    /**
    * @author LazyCat
    * @date  2022/10/12
    * @param
    * @return
     * 后序遍历 递归
    */
//    public void postorder(TreeNode root,List<Integer> result){
//
//        if(root == null){
//            return;
//        }
//
//        postorder(root.left,result);
//        postorder(root.right,result);
//        result.add(root.val);
//    }
}
//leetcode submit region end(Prohibit modification and deletion)

}
